<?php
namespace App\Controllers;
use App\Models\SpongeBob as SpongeBobModel;

class SpongeBob extends BaseController
{
    public function index()
    {
        // 连接到模型
        $spongeBobModel = new SpongeBobModel();
        // 检索所有记录
        $records = $spongeBobModel->findAll();
        
        // 将对象转换为数组
        $records = array_map(function ($record) {
            return (array) $record;
        }, $records);

        // 传递数据给视图
        return view('SpongeBob_body', [
            'title' => 'SpongeBob Destinations',
            'records' => $records
        ]);
    }

    
    public function edit($id = NULL) 
    { 
        $SpongeBob = new \App\Models\SpongeBob();
        $record = $SpongeBob->find($id);
        $fields = [
                'title' => 'Edit a Destination',
                'heading' => 'Edit a Destination',
                'footer' => 'Copyright Xiezhaokai'
                ];
                $parser = \Config\Services::parser();
                return $parser->setData($fields)
                    ->render('templates\top') .
                $parser->setData($record)
                    ->render('editSpongeBob') .
                $parser->setData($fields)
                    ->render('templates\bottom');
    }

    public function handle() { 
        $SpongeBob = new \App\Models\SpongeBob();
            helper(['form', 'url']);
            $record = $SpongeBob->find($this->request->getPost('id'));
     if ($this->validate($SpongeBob->validationRules)) {
        $SpongeBob->save([
                'id' => $this->request->getPost('id'),
                'name' => $this->request->getPost('name'),
                'description' => $this->request->getPost('description'),
                'image' => $this->request->getPost('image'),
                'team' => $this->request->getPost('team'),
                'position' => $this->request->getPost('position'),
                'specialty' => $this->request->getPost('specialty'),
                'favorite_activity' => $this->request->getPost('favorite_activity'),
                ]);
                // id,name,description,image,team,position,specialty,favorite_activity
            }
        $fields = [
                'title' => 'Edit a Destination',
                'heading' => 'Edit a Destination',
                'footer' => 'Copyright Xiezhaokai'
            ];
            $parser = \Config\Services::parser();
            
             //Top of page
            $returnView = $parser->setData($fields)->render('templates\top');
    
            //Body of page
            if (!$this->validate($SpongeBob->validationRules)) {
                $returnView .= $parser->renderString($this->validator->listErrors());
                $returnView .= $parser->setData($this->request->getPost())
                    ->render('editSpongebob');
                } else {
                $returnView .= $parser->renderString("<p>Success!</p>");
                $returnView .= $parser->setData($this->request->getPost())
                    ->render('editSpongeBob');
                }
             
                //Bottom of page
                    $returnView .= $parser->setData($fields)
                    ->render('templates\bottom');
             
                return $returnView;
    }

public function showme($id)
    {
    // connect to the model
    $SpongeBob = new \App\Models\SpongeBob();
    // retrieve all the records
    $record = $SpongeBob->find($id);
    // get a template parser
    $table = new \CodeIgniter\View\Table();

    // $headings = $places->fields;
    // $displayHeadings = array_slice($headings, 1, 2);
    // $table->setHeading(array_map('ucfirst', $displayHeadings));

    $table->addRow('<p><a href="/SpongeBob/edit/'.$record['id'].'">Edit</a ></p >');
    $table->addRow('<p><a href="/SpongeBob/create/'.$record['id'].'">create</a ></p >');
    $table->addRow('Id',$record['id']);
    $table->addRow('Name',$record['name']);
    $table->addRow('Description',$record['description']);
    // $table->addRow('Link',$record['link']);
    $table->addRow('Image','<img src="\\image\\'.$record['image'].'">');
    $table->addRow('Team,',$record['team']);
    $table->addRow('position,',$record['position']);
    $table->addRow('specialty,',$record['specialty']);
    $table->addRow('favorite_activity,',$record['favorite_activity']);
    $table->addRow('<p><a href="/SpongeBob">home</a ></p >');
    // id,name,description,image,team,position,specialty,favorite_activity

    $template = [
        'table_open' => '<table cellpadding="20px" style="margin-top:30px; margin-bottom:30px;">',
        'cell_start' => '<td style="border: 1px solid #dddddd;">',
        'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);
        
        $fields = [
            'title' => ''.$record['name'].'',
            'heading' => ''.$record['name'].'',
            'footer' => 'Copyright Xiezhaokai'
        ];
    // get a template parser 
    $parser = \Config\Services::parser(); 
    // tell it about the substitions 
    // return $parser->setData($record) 
    // // and have it render the template with those ->render('oneplace');
    // ->render('oneplace'); 

    return $parser->setData($fields)
    ->render('templates\top') .
        $table->generate() .
    $parser->setData($fields)
    ->render('templates\bottom');
    }
    public function create($id = NULL)
            {
            $fields = [
                'title' => 'Create a Destination',
                'heading' => 'Create a Destination',
                'footer' => 'Copyright xiezhaokai'
            ];
            $record = [
                'id' => 0,
                'name' => '',
                'description' => '',
                'link' => '',
                'image' => '',
            ];
            $parser = \Config\Services::parser();
            return $parser->setData($fields)
                ->render('templates\top') .
            $parser->setData($record)
                ->render('editplace') .
            $parser->setData($fields)
                ->render('templates\bottom');
            }
        public function delete($id)
            {
            $places = new \App\Models\Places();
            $places->delete($id);
            return redirect()->to('travel');
            }

}